package org.dromara.business.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();

        // 配置消息转换器
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        // 添加Jackson消息转换器用于处理JSON
        messageConverters.add(new MappingJackson2HttpMessageConverter());
        restTemplate.setMessageConverters(messageConverters);

        // 配置拦截器（可选）
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        // 可以添加自定义拦截器，如日志拦截器、认证拦截器等
        // interceptors.add(new LoggingInterceptor());
        // interceptors.add(new AuthInterceptor());
        restTemplate.setInterceptors(interceptors);

        return restTemplate;
    }
}
